﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Common;

public partial class website_admin_system_regionmagviewer : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin();

        switch (RequestBase.GetFormString("otype"))
        {
            case "loadingtreedata":
                LoadingTreeData();
                break;
            case "delete":
                Delete();
                break;
        }
    }

    protected string CategoryTree = "";
    /// <summary>
    /// 异步加载目录树
    /// </summary>
    void LoadingTreeData()
    {
        try
        {
            string parentid = RequestBase.GetFormString("parentid");

            if (!Utils.IsNumber(parentid))
                CategoryTree = "";
            else
            {
                DataTable datCategoryTable = BLL.CommonBLL.Select("RegionBase", "*", " AND ParentId = " + parentid + " AND DeletionStateCode = 0 ORDER BY RegionName ASC");

                /// 加载WBS树
                InitTree(datCategoryTable, int.Parse(parentid));
            }
            BLL.AjaxHelper.ResponseWrite(CategoryTree);
        }
        catch (Exception err) { }
    }

    void InitTree(DataTable dat, int parentId)
    {
        /// 父节点
        string htmlItem1 = "<tr id=\"tr_node_{1}\" class=\"trTreeBoxHeader\" onmouseover=\"this.style.borderColor='#C0C0C0';this.style.color='#000000';this.style.backgroundColor ='#C3DDFF'\" onmouseout=\"this.style.borderColor='';this.style.color='';this.style.backgroundColor =''\">";
        htmlItem1 += "<td><img src=\"../_imgs/admin/plus.gif\" vtype=\"plus\" onclick=\"TreeNoteShow(this);\" notetype=\"{0}\" regionid=\"{1}\" layer=\"{2}\" parentid=\"{3}\" oname=\"imgTreeNoteIco\" />";
        htmlItem1 += "<div>{4}</div><!--名称--></td>";
        htmlItem1 += "<td>{5}<!--编号--></td>"; 
        htmlItem1 += "<td><span>{6}</span><!--操作--></td>";
        htmlItem1 += "</tr>";

        /// 子节点
        string htmlItem2 = "<tr class=\"trTreeBoxHeader\" onmouseover=\"this.style.borderColor='#C0C0C0';this.style.color='#000000';this.style.backgroundColor ='#C3DDFF'\" onmouseout=\"this.style.borderColor='';this.style.color='';this.style.backgroundColor ='#ffffff'\" style=\"background: #ffffff;\">";
        htmlItem2 += "<td><img src=\"../_imgs/admin/defaultnode.gif\" parentid=\"{0}\" />";
        htmlItem2 += "<div>{1}</div><!--名称--></td>";
        htmlItem2 += "<td>{2}<!--编号--></td>"; 
        htmlItem2 += "<td><span>{3}</span><!--操作--></td></tr>";

        if (dat == null)
            return;

        DataRow[] dr = dat.Select("ParentId = " + parentId.ToString());
        if (dr.Length > 0)
        {
            int layer = int.Parse(dat.Select("ParentId = " + parentId.ToString())[0]["Layer"].ToString());

            int tbWidth = 782 - (layer * 20 + 1 + layer);

            string bgcolor = "#ffffff";
            string fontcolor = "#333333";

            /// 如果是根节点
            if (parentId == 0)
                CategoryTree += "<tr class=\"trTreeAreaItems\" id=\"tr_" + parentId.ToString() + "\"><td colspan=\"3\" class=\"body\" style=\"border-bottom: 0px; height: 100%;\">";
            else
                CategoryTree += "<tr class=\"trTreeBoxItems\" id=\"tr_" + parentId.ToString() + "\"><td colspan=\"3\" class=\"body\">";

            CategoryTree += "<table class=\"tbTreeBox\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\" style=\"width: " + tbWidth + "px; background:" + bgcolor + "; color: " + fontcolor + ";\"><!--顶级970px,按层级递减1px-->";
            CategoryTree += "<colgroup>";
            CategoryTree += "<col />";
            CategoryTree += "<col style=\"width: 120px; text-align: right; padding-right: 5px;\" />"; 
            CategoryTree += "<col style=\"width: 120px; text-align: center; padding-left: 10px;\" />";
            CategoryTree += "</colgroup><tbody>";

            foreach (DataRow d in dr)
            {
                if (d["NodeType"].ToString().ToLower().Trim() == "childnode" && layer > 1)
                {
                    CategoryTree += string.Format(htmlItem2,
                        d["ParentId"].ToString(),
                        d["RegionName"].ToString(),
                        string.IsNullOrEmpty(d["RegionNameEng"].ToString().Trim()) ? "&nbsp;" : d["RegionNameEng"].ToString().ToUpper(),
                        "<img src=\"../_imgs/ico/pencil.png\" style=\"cursor: pointer; border: 0px; margin: 5px 5px;\" alt=\"修改地区信息\" onclick=\"Modify('" + d["RegionId"].ToString() + "')\" /><img src=\"../_imgs/ico/pencil_add.png\" style=\"cursor: pointer; border: 0px; margin: 5px 8px;\" alt=\"添加下级地区\" onclick=\"OpenDia('addchildnode', '" + d["RegionId"].ToString() + "')\" /><img src=\"../_imgs/ico/pencil_delete.png\" style=\"cursor: pointer; border: 0px; margin: 5px 8px;\" alt=\"删除地区\" onclick=\"Delete('" + d["RegionId"].ToString() + "');\" />");
                }
                else
                {
                    CategoryTree += string.Format(htmlItem1,
                        d["NodeType"].ToString(),
                        d["RegionId"].ToString(),
                        d["Layer"].ToString(),
                        d["ParentId"].ToString(),
                        d["RegionName"].ToString(),
                        string.IsNullOrEmpty(d["RegionNameEng"].ToString().Trim()) ? "&nbsp;" : d["RegionNameEng"].ToString().ToUpper(),
                        "<img src=\"../_imgs/ico/pencil.png\" style=\"cursor: pointer; border: 0px; margin: 5px 5px;\" alt=\"修改地区信息\" onclick=\"Modify('" + d["RegionId"].ToString() + "')\" /><img src=\"../_imgs/ico/pencil_add.png\" style=\"cursor: pointer; border: 0px; margin: 5px 8px;\" alt=\"添加下级地区\" onclick=\"OpenDia('addchildnode', '" + d["RegionId"].ToString() + "')\" /><img src=\"../_imgs/ico/pencil_delete.png\" style=\"cursor: pointer; border: 0px; margin: 5px 8px;\" alt=\"删除地区\" onclick=\"Delete('" + d["RegionId"].ToString() + "');\" />");
                }

                InitTree(dat, int.Parse(d["RegionId"].ToString().Trim()));
            }

            CategoryTree += "</tbody></table></td></tr>";
        }
    }
    /// <summary>
    /// 删除目录
    /// </summary>
    void Delete()
    {
        string res = BLL.RegionBase.Delete(
            int.Parse(RequestBase.GetFormString("objectId")),
            true
            );

        BLL.AjaxHelper.ResponseWrite(res);
    }
}